package fun.werfamily.wf.goods.core.domain.service.impl;

import fun.werfamily.wf.goods.core.domain.constant.RedisConstant;
import fun.werfamily.wf.goods.core.domain.constant.RedisTimeoutConstants;
import fun.werfamily.wf.goods.core.domain.dto.request.BannerListReqDTO;
import fun.werfamily.wf.goods.core.domain.dto.request.BannerReqDTO;
import fun.werfamily.wf.goods.core.domain.dto.response.BannerDTO;
import fun.werfamily.wf.goods.core.domain.enums.BannerStatusEnum;
import fun.werfamily.wf.goods.core.domain.service.HomeService;
import fun.werfamily.wf.goods.core.domain.service.IBannerService;
import fun.werfamily.wf.goods.core.domain.util.RedisUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Mr.WenMing Zhang
 * @Date: 2022/06/28 16:52
 * @Description:
 **/
@Slf4j
@Service
public class HomeServiceImpl implements HomeService {
    @Resource
    private RedisUtil redisUtil;

    @Autowired
    private IBannerService bannerService;

    /**
     * banner列表
     *
     * @param bannerReqDTO
     * @return
     */
    @Override
    public List<BannerDTO> listBanner(BannerReqDTO bannerReqDTO) {
        BannerListReqDTO bannerListReqDTO = new BannerListReqDTO();
        bannerListReqDTO.setPosition(bannerReqDTO.getPosition());
        bannerListReqDTO.setClientTypeList(Collections.singletonList(bannerReqDTO.getClientType()));
        bannerListReqDTO.setStatus(BannerStatusEnum.NORMAL.getStatus());
        long currentTimestamp = System.currentTimeMillis();
        // 获取banner缓存
        List<BannerDTO> bannerDTOList = redisUtil.getListByRange(RedisConstant.BANNER_LIST_CLIENT_TYPE_POSITION + bannerReqDTO.getClientType() + bannerReqDTO.getPosition(), 0L, -1L, BannerDTO.class);
        if (CollectionUtils.isEmpty(bannerDTOList)) {
            try {
                bannerDTOList = bannerService.listByParams(bannerListReqDTO);
                if (!CollectionUtils.isEmpty(bannerDTOList)) {
                    redisUtil.listRightPushAll(RedisConstant.BANNER_LIST_CLIENT_TYPE_POSITION + bannerReqDTO.getClientType() + bannerReqDTO.getPosition(), bannerDTOList);
                    redisUtil.setExpire(RedisConstant.BANNER_LIST_CLIENT_TYPE_POSITION + bannerReqDTO.getClientType() + bannerReqDTO.getPosition(), RedisTimeoutConstants.CACHE_HOLD_TIME_1TO3H);
                }
            } catch (Exception e) {
                log.error("banner列表异常", e);
                return new ArrayList<>();
            }
        }
        if (!CollectionUtils.isEmpty(bannerDTOList)) {
            // 去除过期banner
            bannerDTOList = bannerDTOList.stream().filter(
                    bannerDTO -> null == bannerDTO.getEndTime()
                            || (currentTimestamp <= bannerDTO.getEndTime().getTime() && currentTimestamp >= bannerDTO.getStartTime().getTime()
                    )).collect(Collectors.toList());
        }
        return bannerDTOList;
    }
}
